package constant.milk.cashbook.sms;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import constant.milk.cashbook.bean.SmsData;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

public class SmsGet
{
Context context;
	
	public SmsGet(Context context)
	{
		this.context = context;
	}
	
	public ArrayList<SmsData> getSms(long lastDate ,long fristDate)
	{
		
		String [] selectionArgs = new String[]{fristDate + "", lastDate + ""};
		
		String address = "";
		String body = "";
		String type = "";
		String date = "";
		
		ArrayList<String> cardName = new ArrayList<String>();
		cardName = new SmsNameData().getCardName();
		
		ArrayList<SmsData> list = new ArrayList<SmsData>();
		ArrayList<SmsData> invertList = new ArrayList<SmsData>();
		
		
		// 갤 씨리즈
		try 
		{
			Cursor cursor = context.getContentResolver().query(Uri.parse("content://com.sec.mms.provider/message/"), 
					null, "RegTime >= ? AND RegTime <= ?", selectionArgs, null);
			
			if (cursor != null)
			{
				cursor.moveToFirst();
				
				for (int i = 0; i < cursor.getCount(); i++) 
				{
					try 
					{
						type = cursor.getString(cursor.getColumnIndex("MainType"));
						
						if (type.equals("0")) {

							date = cursor.getString(cursor.getColumnIndex("RegTime"));
							Date date2 = new Date(Long.parseLong(date));
							SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
							date = sdf.format(date2);
							address = cursor.getString(cursor.getColumnIndex("MDN1st"));
							address = address.replaceAll("-", "");
							body = cursor.getString(cursor.getColumnIndex("Title"));
							
							for (int j=0; j<cardName.size(); j++) 
							{
								if (body.indexOf(cardName.get(j)) != -1) 
								{
									SmsData data = new SmsData();
									data.setDate(date);
									data.setAddress(address);
									data.setMemo(body);
									list.add(data);
									break;
								}
							}
						}
					} catch (Exception e) {
					} finally {
						cursor.moveToNext();
					}
				}

				invertList = new ArrayList<SmsData>();
				for (int i=list.size()-1; i>-1; i--) {
					invertList.add(list.get(i));
				}
				
				cursor.close();
				
				return invertList;
				
			}
		} catch (Exception e) {
		}
		
		// 몰라
		try {
			Cursor cursor = context.getContentResolver().query(Uri.parse("content://com.sec.mms.provider/message"), 
					null, "RegTime >= ? AND RegTime <= ?", selectionArgs, null);
			
			if (cursor != null)
			{
				cursor.moveToFirst();
				
				for (int i = 0; i < cursor.getCount(); i++) 
				{
					try 
					{
						type = cursor.getString(cursor.getColumnIndex("MainType"));
						
						if (type.equals("0")) {

							date = cursor.getString(cursor.getColumnIndex("RegTime"));
							Date date2 = new Date(Long.parseLong(date));
							SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
							date = sdf.format(date2);
							address = cursor.getString(cursor.getColumnIndex("MDN1st"));
							address = address.replaceAll("-", "");
							body = cursor.getString(cursor.getColumnIndex("Title"));
							
							for (int j=0; j<cardName.size(); j++) 
							{
								if (body.indexOf(cardName.get(j)) != -1) 
								{
									SmsData data = new SmsData();
									data.setDate(date);
									data.setAddress(address);
									data.setMemo(body);
									list.add(data);
									break;
								}
							}
						}
					} catch (Exception e) {
					} finally {
						cursor.moveToNext();
					}
				}

				invertList = new ArrayList<SmsData>();
				for (int i=list.size()-1; i>-1; i--) {
					invertList.add(list.get(i));
				}
				
				cursor.close();
				
				return invertList;
				
			}
		} catch (Exception e) {
		}
		
		// 몰라
		try {
			Cursor cursor = context.getContentResolver().query(Uri.parse("content://com.kt.mmsclient/messages"), 
					null, "RegTime >= ? AND RegTime <= ?", selectionArgs, null);
			
			if (cursor != null)
			{
				cursor.moveToFirst();
				
				for (int i = 0; i < cursor.getCount(); i++) 
				{
					try 
					{
						address = cursor.getString(cursor.getColumnIndex("Callback"));
						address = address.replaceAll("-", "");
						body = cursor.getString(cursor.getColumnIndex("Content"));
						
						for (int j=0; j<cardName.size(); j++) 
						{
							if (body.indexOf(cardName.get(j)) != -1) 
							{
								SmsData data = new SmsData();
								data.setAddress(address);
								data.setMemo(body);
								list.add(data);
								break;
							}
						}
					} catch (Exception e) {
					} finally {
						cursor.moveToNext();
					}
				}

				invertList = new ArrayList<SmsData>();
				for (int i=list.size()-1; i>-1; i--) {
					invertList.add(list.get(i));
				}
				
				cursor.close();
				
				return invertList;
				
			}
		} catch (Exception e) {
		}
			
		// 갤A
		try {
			Cursor cursor = context.getContentResolver().query(Uri.parse("content://com.btb.sec.mms.provider/message/"), 
					null, "RegTime >= ? AND RegTime <= ?", selectionArgs, null);
			
			if (cursor != null)
			{
				cursor.moveToFirst();
				
				for (int i = 0; i < cursor.getCount(); i++) 
				{
					try 
					{
						date = cursor.getString(cursor.getColumnIndex("RegTime"));
						Date date2 = new Date(Long.parseLong(date));
						SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
						date = sdf.format(date2);
						address = cursor.getString(cursor.getColumnIndex("MDN1st"));
						address = address.replaceAll("-", "");
						body = cursor.getString(cursor.getColumnIndex("Title"));
						
						for (int j=0; j<cardName.size(); j++) 
						{
							if (body.indexOf(cardName.get(j)) != -1) 
							{
								SmsData data = new SmsData();
								data.setDate(date);
								data.setAddress(address);
								data.setMemo(body);
								list.add(data);
								break;
							}
						}
					} catch (Exception e) {
					} finally {
						cursor.moveToNext();
					}
				}

				invertList = new ArrayList<SmsData>();
				for (int i=list.size()-1; i>-1; i--) {
					invertList.add(list.get(i));
				}
				
				cursor.close();
				
				return invertList;
				
			}
		} catch (Exception e) {
		}
				
		// LG
		try {
			Cursor cursor = context.getContentResolver().query(Uri.parse("content://com.lge.messageprovider/msg/inbox/"), 
					null, "date >= ? AND date <= ?", selectionArgs, null);
			
			if (cursor != null)
			{
				cursor.moveToFirst();
				
				for (int i = 0; i < cursor.getCount(); i++) 
				{
					try 
					{
						date = cursor.getString(cursor.getColumnIndex("date"));
						Date date2 = new Date(Long.parseLong(date));
						SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
						date = sdf.format(date2);
						address = cursor.getString(cursor.getColumnIndex("sender"));
						address = address.replaceAll("-", "");
						body = cursor.getString(cursor.getColumnIndex("body"));
						
						for (int j=0; j<cardName.size(); j++) 
						{
							if (body.indexOf(cardName.get(j)) != -1) 
							{
								SmsData data = new SmsData();
								data.setDate(date);
								data.setAddress(address);
								data.setMemo(body);
								list.add(data);
								break;
							}
						}
					} catch (Exception e) {
					} finally {
						cursor.moveToNext();
					}
				}
				
				invertList = new ArrayList<SmsData>();
				for (int i=list.size()-1; i>-1; i--) {
					invertList.add(list.get(i));
				}
				
				cursor.close();
				
				return invertList;
				
			}
		} catch (Exception e) {
		}
		
		// LG op2x
		try {
			Cursor cursor = context.getContentResolver().query(Uri.parse("content://com.btb.ums.provider.MessageProvider/sms"), 
					null, "DeliveryTime >= ? AND DeliveryTime <= ?", selectionArgs, null);
			
			if (cursor != null)
			{
				cursor.moveToFirst();
				
				for (int i = 0; i < cursor.getCount(); i++) 
				{
					try 
					{
						date = cursor.getString(cursor.getColumnIndex("DeliveryTime"));
						Date date2 = new Date(Long.parseLong(date));
						SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
						date = sdf.format(date2);
						address = cursor.getString(cursor.getColumnIndex("MDN1nd"));
						address = address.replaceAll("-", "");
						body = cursor.getString(cursor.getColumnIndex("Title"));
						
						for (int j=0; j<cardName.size(); j++) 
						{
							if (body.indexOf(cardName.get(j)) != -1) 
							{
								SmsData data = new SmsData();
								data.setDate(date);
								data.setAddress(address);
								data.setMemo(body);
								list.add(data);
								break;
							}
						}
					} catch (Exception e) {
					} finally {
						cursor.moveToNext();
					}
				}
				
				invertList = new ArrayList<SmsData>();
				for (int i=list.size()-1; i>-1; i--) {
					invertList.add(list.get(i));
				}
				
				cursor.close();
				
				return invertList;
				
			}
		} catch (Exception e) {
		}
		
		// 일반
		try {
			Cursor cursor = context.getContentResolver().query(Uri.parse("content://sms/inbox"), 
					null, "date >= ? AND date <= ?", selectionArgs, null);
			
			if (cursor != null)
			{
				cursor.moveToFirst();
				
				for (int i = 0; i < cursor.getCount(); i++) 
				{
					try 
					{
						date = cursor.getString(cursor.getColumnIndex("date"));
						Date date2 = new Date(Long.parseLong(date));
						SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
						date = sdf.format(date2);
						address = cursor.getString(cursor.getColumnIndex("address"));
						address = address.replaceAll("-", "");
						body = cursor.getString(cursor.getColumnIndex("body"));
						
						for (int j=0; j<cardName.size(); j++) 
						{
							if (body.indexOf(cardName.get(j)) != -1) 
							{
								SmsData data = new SmsData();
								data.setDate(date);
								data.setAddress(address);
								data.setMemo(body);
								list.add(data);
								break;
							}
						}
					} catch (Exception e) {
					} finally {
						cursor.moveToNext();
					}
				}
				
				cursor.close();
				
				return list;
				
			}
		} catch (Exception e) {
		}
		
		return list;
	}
}
